package soar.template.spring.msg.batch.reward.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.stereotype.Component;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
public class MySQL
{
//    @Autowired
//    @Qualifier("appProps")
//    private Properties appProps;

    @Autowired
    private MySQLPropertiesDB1 mysqlPropertiesDB1;
    
    @Bean
    @Primary
    public DataSource datasourceDB1() throws Exception
    {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.mysql.jdbc.Driver");
//      String prefix="mysql.db1.";
//        ds.setJdbcUrl(appProps.getProperty(prefix+"url"));
//        ds.setUser(appProps.getProperty(prefix+"username"));
//        ds.setPassword(appProps.getProperty(prefix+"password"));
        ds.setJdbcUrl(mysqlPropertiesDB1.getUrl());
        ds.setUser(mysqlPropertiesDB1.getUsername());
        ds.setPassword(mysqlPropertiesDB1.getPassword());
        ds.setMaxPoolSize(100);
        ds.setMaxIdleTime(20);
        return ds;
    }

    @Bean(name = "emfDB1")
    @Primary
    public LocalContainerEntityManagerFactoryBean emfDB1() throws Exception
    {
        HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
        vendor.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        vendor.setGenerateDdl(false);
        vendor.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceUnitName("db1");
        factory.setDataSource(datasourceDB1());
        factory.setJpaVendorAdapter(vendor);
        Properties props = new Properties();
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.hbm2ddl.auto", "validate");
        factory.setJpaProperties(props);

        return factory;
    }

    @Autowired
    private MySQLPropertiesDB2 mysqlPropertiesDB2;
    
    @Bean
    public DataSource datasourceDB2() throws Exception
    {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        ds.setDriverClass("com.mysql.jdbc.Driver");
        ds.setJdbcUrl(mysqlPropertiesDB2.getUrl());
        ds.setUser(mysqlPropertiesDB2.getUsername());
        ds.setPassword(mysqlPropertiesDB2.getPassword());
        ds.setMaxPoolSize(100);
        ds.setMaxIdleTime(20);
        return ds;
    }

    @Bean(name = "emfDB2")
    public LocalContainerEntityManagerFactoryBean emfDB2() throws Exception
    {
        HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
        vendor.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
        vendor.setGenerateDdl(false);
        vendor.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setPersistenceUnitName("db2");
        factory.setDataSource(datasourceDB2());
        factory.setJpaVendorAdapter(vendor);
        Properties props = new Properties();
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.hbm2ddl.auto", "validate");
        factory.setJpaProperties(props);

        return factory;
    }
}

class MySQLProperties
{
    protected String url;
    protected String username;
    protected String password;

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

}

@Component
@ConfigurationProperties("mysql.db1")
class MySQLPropertiesDB1 extends MySQLProperties
{

}

@Component
@ConfigurationProperties("mysql.db2")
class MySQLPropertiesDB2 extends MySQLProperties
{

}
